package icu.ac.kr.phonebook.activity;



import icu.ac.kr.phonebook.R;
import icu.ac.kr.phonebook.provider.ConstantProvider;
import icu.ac.kr.phonebook.view.CalendarView;
import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class RegisterScheduleActivity extends Activity {

	CalendarView calendarView;
	TextView month_title;
	TextView year_title;
	
	static final String TAG = "RegisterScheduleActivity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	
		 setContentView(R.layout.viewmonth);
		 calendarView = (CalendarView) findViewById(R.id.calendarview);
		 month_title = (TextView) findViewById(R.id.month_title);
		 year_title = (TextView) findViewById(R.id.year_title);
		 
		 setTitle();
		 ImageButton previous = (ImageButton) findViewById(R.id.previous_btn);
	 	 previous.setOnClickListener(mClicklistener);

	 	ImageButton next = (ImageButton) findViewById(R.id.next_btn);
		 next.setOnClickListener(mClicklistener);
		 
		 ImageButton previousYear = (ImageButton) findViewById(R.id.previousYearBtn);
	 	 previousYear.setOnClickListener(mClicklistener);

	 	ImageButton nextYear = (ImageButton) findViewById(R.id.nextYearBtn);
		 nextYear.setOnClickListener(mClicklistener);
		 
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		calendarView.onResume();
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onContextItemSelected(item);
	
		int selectedId = item.getItemId();
		int selectedDate = selectedId & 31;
		long dbId = selectedId >> 8;
		
		int mode = 0;
		
		if ( (selectedId & ConstantProvider.MODEMASK) == ConstantProvider.PRIVATE)
		{
			Log.d(TAG, "Item PRIVATE MODE " + dbId );
			mode = 0;
			
		}
		else if ((selectedId & ConstantProvider.MODEMASK) == ConstantProvider.SHARE  )
		{
			Log.d(TAG, "Item SHARABLE MODE " + dbId );
			mode = 3;
			
		}
		else if ((selectedId & ConstantProvider.MODEMASK) == ConstantProvider.PUBLIC  )
		{
			mode = 1;
			Log.d(TAG, "Item PUBLIC MODE" + dbId );
		}
		else if ((selectedId & ConstantProvider.MODEMASK) == ConstantProvider.COUPLE  )
		{
			mode = 2;
			Log.d(TAG, "Item COUPLE MODE" + dbId);
		}
		
		ContentValues values = new ContentValues();
		values.put("mode", mode);
		getContentResolver().update(ConstantProvider.MONTHMODE_URI, values, "_id="+dbId, null);
		if ( mode != 3 )
			getContentResolver().update(ConstantProvider.TOTALSCHEDULE_URI, values, "year="+calendarView.getYear()+" and month="+calendarView.getMonth()+" and date ="+selectedDate, null);
		
			
		onResume();
		return true;
	}


	public void setTitle(){
		
		month_title.setText("" +ConstantProvider.getMonth(calendarView.getMonth()));
		year_title.setText("" +calendarView.getYear());
		
	}
	
	Button.OnClickListener mClicklistener = new Button.OnClickListener(){

		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId())
			{
			case R.id.previous_btn:
				calendarView.moveDownMonth();
				break;
			case R.id.next_btn:
				calendarView.moveUpMonth();
				break;
			case R.id.previousYearBtn:
				calendarView.moveDownYear();
				break;
			case R.id.nextYearBtn:
				calendarView.moveUpYear();
				break;
			}

			setTitle();

		}
	 	
	};
	
	
	
}
